package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.InvoiceDAO;
import entity.Invoice;

@WebServlet("/invoice")
public class InvoiceServlet extends HttpServlet {
	private InvoiceDAO invoicedao = new InvoiceDAO();
	private String action;

	public InvoiceServlet() {
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		this.action = request.getParameter("action");
		if (action.equals("delt")) {
			invoice_delt(request);
		} else if(action.equals("edit")) {
			invoice_edit(request);
		}
		request.getRequestDispatcher("/invoiceList.jsp").forward(request, response);
	}

	private void invoice_edit(HttpServletRequest request) {
		try {
			 Invoice fp=new Invoice();
			 fp.setPno(request.getParameter("pno"));
			 fp.setCno(request.getParameter("cno"));
			 fp.setPaystate(request.getParameter("paystate"));
			 fp.setPprice(request.getParameter("pprice"));
			 fp.setPaytimeno(request.getParameter("paytimeno"));
			 fp.setPtime(request.getParameter("ptime"));
	   		this.invoicedao.editInvoice(fp);

		 } catch (Exception ex) {
			 
	         request.setAttribute("errormsg", ex.getMessage());
	         
	     }
	}

	private void invoice_delt(HttpServletRequest request) {
		 try {
	   		this.invoicedao.deltInvoice(request.getParameter("pno"));

		 } catch (Exception ex) {
			 
	         request.setAttribute("errormsg", ex.getMessage());
	         
	     }
		
	}

}
